\NeedsTeXFormat{LaTeX2e}
%%%
%%% xytree.sty
%%%
%%% Ver 1.0 xytree.sty written by Koaunghi Un <2004/08/10>
%%% Ver 1.1 modified by Koaunghi Un <2004/11/05>
%%%         \barnodeconnect with label
%%% Ver 1.2 added two new macros by Koaunghi Un <2004/12/20>
%%%         \yytree: a new tree macro
%%%         \xynodefont: font changing macro for all nodes in \xytree
%%%         and \yytree.
%%% Ver 1.3 added three new macros by Koaunghi Un <2006/02/27>
%%%         for drawing diagrams for Discourse Representation Structure
%%%         \drsbox: with referent and predicate
%%%         \drsrectbox: only for predicate
%%%         \drsdiabox: diamond-shaped box for DRS conditions.
%%% Ver 1.4 Bug fixed by Koaunghi Un <2006/03/07>
%%%         \drop'ing a frame after \xytrinode shifted the reference
%%%         point to the lower right corner of the triangle.
%%% Ver 1.5 correct font change of \xyterminal by Koaunghi Un <2006/08/15>
%%% 	    italic font shape of \xyterminal after \\
%%%

\ProvidesPackage{xytree}[2006/8/15 Pacakge for drawing trees]
\typeout{`xytree.sty' package v1.5 for drawing trees.}

\RequirePackage[all]{xy}

%%
%% \xytree[column spacing in pc]{matrix}
%% -------
%% A tree is a simple macro of the form "\xytree{...}"
%% It is an xymatrix, with "&" as column separator and "\\" as row
%% separator:
%%
%% \rowheight: sets row spacing.
%%

\newdimen\rowheight\newdimen\XyColumnSpacing \rowheight2pc
\makeatother
\newcommand{\xytree}[2][0.5]{\XyColumnSpacing=#1pc\relax%
  \ifdim\XyColumnSpacing>0pt\relax\else\XyColumnSpacing=0.01.pc\fi%
  \rule{0pt}{0pt}%
  \xymatrix @R=\rowheight @!C=\XyColumnSpacing @M=0pc {#2}}
\makeatletter

%%
%% \xyconnect[line shape]
%%      [line bend]
%%      (source corner, target corner)
%%      {target row, target col}
%%      "label"
%%

\def\xyconnect{\@ifnextchar[{\n@deconnect}{\n@deconnect[-]}}%]
\def\n@deconnect[#1]{\@ifnextchar[{\n@dec@nnect[#1]}%]
  {\n@dec@nnect[#1][\empty]}}
\def\n@dec@nnect[#1][#2]{\@ifnextchar({\n@d@c@nnect[#1][#2]}%)
  {\n@d@c@nnect[#1][#2](D,U)}}
\def\n@d@c@nnect[#1][#2](#3)#4{\@ifnextchar"{\connectwitharrow[#1][#2](#3)#4}%
  {\connectwitharrow[#1][#2](#3)#4""}}
\makeatother
\def\connectwitharrow[#1][#2](#3,#4)#5"#6"{%
  \ifx\empty#2\relax
  \ar @{#1} []+#3;[#5]+#4 #6
  \else
  \ar @{#1} @/#2/ []+#3;[#5]+#4 #6
  \fi}
\makeatletter

\def\labelstyle{\hbox}
\def\ind#1{{\fboxsep1pt\fbox{\small#1}}}

\def\@drawlines#1,#2\relax{%
  \xyconnect{1,#1}%
  \ifx#2\@empty\else\@drawlines#2\relax\fi%
}

%%
%% Tree Node
%%
%% \xynode[connecting nodes delimited by ,]{entry}
%% \xytrinode[+/-triangle width]{entry}
%% \xyterminal{entry}
%%
%% Following code
%%   \xynode{ & xynode[-1,1]{S} \\
%%           \xynode[0]{NP} & & \xytrinode{VP} \\
%%           \xyterminal{I} & & \xyterminal{ate an apple}
%%   }
%% will draw following tree diagram:
%%          S
%%         /\
%%        /  \
%%       /    \
%%      NP    VP
%%      |     /\
%%      |    /  \
%%      |    ____
%%      I ate an apple
%% with the terminal nodes written in italic shape.
%% Lines are drawn with the macro \xyconnect defined above.
%%

\def\xynode{\@ifnextchar[{\xyn@de}{\xyn@de[]}}%]
\def\xyn@de[#1]#2{{\txt{{\strut\xyn@def@nt #2}}}%
  \ifx#1\@empty\else\expandafter\@drawlines#1,\relax\fi}
\newcommand{\xytrinode}[2][0]{{\txt{{\strut#2}}}
  \POS[]+D;\POS[d]+UL\POS-(#1,0)**\dir{-}%
  \POS[]+D;\POS[d]+UR\POS+(#1,0)**\dir{-}%
  \POS[]\POS[d]+UL\POS-(#1,0);\POS[]\POS[d]+UR\POS+(#1,0)**\dir{-};\POS[]}
\newcommand{\xyterminal}[1]{\textit{\txt{\strut #1}}}

%%
%% \xytext
%% -------
%% For morph-syntactic agrement "\xytext{...}" is used in place of "\xytree"
%% It is \xytree with one column:
%%

\makeatother
\newcommand{\xytext}[1]{\raisebox{3pt}{\def\objectstyle{\hbox}%
    \xymatrix @C=1ex @M=0pt {#1}}\hspace{-2pt}}
\makeatletter

%%
%% \xybarconnect[line displacement]
%%      [line shape]
%%      (source corner, target corner)
%%      {target row, target col}
%%      "label"
%%

\def\xybarconnect{\@ifnextchar[%]
  {\b@rconnect}{\b@rconnect[3]}}
\def\b@rconnect[#1]{\@ifnextchar[%]
  {\b@rc@nnect[#1]}{\b@rc@nnect[#1][->]}}
\def\b@rc@nnect[#1][#2]{\@ifnextchar(%)
  {\b@rc@nn@ct[#1][#2]}{\b@rc@nn@ct[#1][#2](U,U)}}
\def\b@rc@nn@ct[#1][#2](#3,#4)#5{\@ifnextchar"%
  {\b@rc@nn@ct@[#1][#2](#3,#4)#5}%
  {\b@rc@nn@ct@[#1][#2](#3,#4)#5""}}
\def\b@rc@nn@ct@[#1][#2](#3,#4)#5"#6"{%
  \edef\reserved@a{%
    \csname c@rner@#3\endcsname#1\relax/%
    \csname c@rner@#4\endcsname#1\relax}%
  \expandafter\DrawHorizontal\reserved@a/#1/#2/#3/#4/#5/#6\end\ignorespaces}
\def\c@rner@U#1\relax{#1}
\let\c@rner@UL\c@rner@U
\let\c@rner@UR\c@rner@U
\let\c@rner@R\c@rner@U
\def\c@rner@D#1\relax{-#1}
\let\c@rner@DR\c@rner@D
\let\c@rner@DL\c@rner@D
\let\c@rner@L\c@rner@D
\makeatother
\def\DrawHorizontal#1/#2/#3/#4/#5/#6/#7/#8\end{%
  \ar @{#4} '[0,0]+#5 '[0,0]+#5+(0,#1) '[0,#7]+#6+(0,#2)#8 [0,#7]+#6
}
\makeatletter

%%
%% \xybarnode{entry}
%%
%% Following codes
%%   AGR \xytext{\xybarnode{Det}\xybarconnect{1} & \xybarnode{head-noun} & verb} ...
%% will draw following diagram:
%%            ______
%%           |      |
%%           |     \/
%%      AGR Det head-noun verb ...
%%

\newcommand{\xybarnode}[1]{\strut#1\ignorespaces}

%%
%% \yytree[indentation in pc]{matrix}
%% -------
%%
%% A \yytree is an another tree drawing macro of the form "\yytree{...}"
%% It is an xymatrix, with "&" as column separator and "\\" as row
%% separator:
%%

\makeatother
\newcommand{\yytree}[2][2]{%
  \rule{0pt}{0pt}%
  \xymatrix @!0 @C=#1pc @R=1.5pc @W=0pc @*[r] {#2}
}
\makeatletter

%%
%% \yynode[daughter nodes delimited by ,]{entry}
%% -------
%%
%% For example
%%   \yytree{ \yynode[1,4]{top} \\
%%     & \yyode[1,2]{A} \\
%%     & & \yynode{Aa} \\
%%     & & \yynode{Ab} \\
%%     & \yynode[1]{B} \\
%%     & & \yynode{Ba}
%%   }
%% will produce the following tree:
%%   top
%%    |-- A
%%    |   |-Aa
%%    |   |-Ab
%%    |-- B
%%        |-Ba
%% with proper bending line for the last daughter node.
%%

\def\yynode{\@ifnextchar[{\yyn@de}{\yyn@de[]}}%]
\def\yyn@de[#1]#2{{\txt{{\xyn@def@nt #2}}}%
  \@tempcnta=0
  \ifx#1\@empty\else\expandafter\@yylines#1,\relax\fi%
  \ifnum\@tempcnta>0
  \edef\reserved@a{\noexpand\POS[]+D+(1,0);[\the\@tempcnta,0]+(1,0)**\noexpand\dir{-}}%
  \reserved@a\fi
}

\def\@yylines#1,#2\relax{%
  \POS[#1,0]+(1,0);[#1,1]+L **\dir{-}%
  \@tempcnta=#1
  \ifx#2\@empty\else\@yylines#2\relax\fi%
}

%%
%% \xynodefont
%% -----------
%% Default font for node entries in \xytree and \yytree.
%% This is initially set to be empty, which is the same as \rmfamily
%% \xynodefont{\itshape} typeset all node entries in italic shape.
%%

\def\xynodefont#1{\def\xyn@def@nt{#1}}
\xynodefont{}

%%%
%%% Discourse Representation Structure
%%%
%% This is not related with linguistic trees, but because no other
%% packages provide a convenient way to draw DRS diagram without the
%% help of pstricks, these three macros for drawing DRS diagram are
%% added here.  They use also Xy-pic feature to construct boxes.
%%

\newdimen\drs@wd\newdimen\drs@ht\newdimen\drsboxsep
\drsboxsep6pt
\newbox\drs@boxa\newbox\drs@boxb

%%
%% \drsbox[line art]{referent}{predicate}
%% -------
%% [line art] : optional.  specifies the art of lines.  All \dir
%%   directionals of Xy-pic kernel library can be used (not \dir2,
%%   \dir3, \dir_, \dir^ directionals)
%% referent:
%% predicate:
%%

%% ------------
%% | x        |
%% ------------
%% |          |
%% | donkey(x)|
%% | green(x) |
%% |          |
%% ------------

\newcommand{\drsbox}[3][-]{%
  \begin{tabular}{l}\tabcolsep0pt
    \itshape
    \setbox\drs@boxa\hbox{\begin{tabular}{l}#2\end{tabular}}%
    \drs@wd=\wd\drs@boxa
    \drs@ht=\ht\drs@boxa\advance\drs@ht by\dp\drs@boxa
    \setbox\drs@boxb\hbox{\begin{tabular}{l}#3\end{tabular}}%
    \ifdim\drs@wd<\wd\drs@boxb\drs@wd\wd\drs@boxb\fi
    \advance\drs@ht by\ht\drs@boxb \advance\drs@ht by\dp\drs@boxb
    \divide\drs@ht by2
    \advance\drs@ht by1.5\drsboxsep
    \wd\drs@boxa=\drs@wd\wd\drs@boxb=\drs@wd
    \ifdim\ht\drs@boxa<\bigskipamount\ht\drs@boxa=\bigskipamount\fi
    \xy
    <0pt,0pt>*\hbox{\box\drs@boxb}="b";
    <0pt,\drs@ht>*\hbox{\box\drs@boxa}="a";
    "a"+LU+<-\drsboxsep,.5\drsboxsep>;
    "a"+RU+<\drsboxsep,.5\drsboxsep>**\dir{#1};
    "b"+RD+<\drsboxsep,-\drsboxsep>**\dir{#1};
    "b"+LD+<-\drsboxsep,-\drsboxsep>**\dir{#1};
    "a"+LU+<-\drsboxsep,.5\drsboxsep>**\dir{#1};
    "b"+LU+<-\drsboxsep,\drsboxsep>;"b"+RU+<\drsboxsep,\drsboxsep>**\dir{#1};
    \endxy
  \end{tabular}}

%%
%% \drsrectbox[line art]{content}
%% -----------
%%

%% -------------
%% |           |
%% | man(u)    |
%% | unicorn(u)|
%% | seeks(u,v)|
%% |           |
%% -------------

\newcommand{\drsrectbox}[2][-]{%
  \begin{tabular}{l}\tabcolsep0pt
    \itshape
    \xy
    *\hbox{\begin{tabular}{l}#2\end{tabular}}="a";
    "a"+LD+<-\drsboxsep,-\drsboxsep>;
    "a"+LU+<-\drsboxsep,\drsboxsep>**\dir{#1};
    "a"+RU+<\drsboxsep,\drsboxsep>**\dir{#1};
    "a"+RD+<\drsboxsep,-\drsboxsep>**\dir{#1};
    "a"+LD+<-\drsboxsep,-\drsboxsep>**\dir{#1};
    \endxy
  \end{tabular}}

%%
%% \drsdiabox[line art]{conditions}
%% ----------
%%

%%      /\
%%     /  \
%%    /    \
%%   /      \
%%  /        \
%% /  every   \
%% \    x     /
%%  \        /
%%   \      /
%%    \    /
%%     \  /
%%      \/

\newcommand{\drsdiabox}[2][-]{%
  \begin{tabular}{l}\tabcolsep0pt
    \itshape
    \setbox\drs@boxa\hbox{\begin{tabular}{c}#2\end{tabular}}%
    \@tempdima=\wd\drs@boxa
    \@tempdima=.5\wd\drs@boxa
    \advance\@tempdima by.5\ht\drs@boxa
    \advance\@tempdima by.5\drsboxsep
    \xy
    *\hbox{\box\drs@boxa}="a";
    "a"+<-\@tempdima,0pt>;
    "a"+<0pt,\@tempdima>**\dir{#1};
    "a"+<\@tempdima,0pt>**\dir{#1};
    "a"+<0pt,-\@tempdima>**\dir{#1};
    "a"+<-\@tempdima,0pt>**\dir{#1};
    \endxy
  \end{tabular}}

%%%
%%% Autosegmental Representation
%%% ----------------------------
%%%


\def\asrnode#1{\txt{\strut#1}}

\endinput
